{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo for the DoWhy causal API\n",
    "We show a simple example of adding a causal extension to any dataframe. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dowhy.datasets\n",
    "import dowhy.api\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from statsmodels.api import OLS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.883800</td>\n",
       "      <td>True</td>\n",
       "      <td>6.032204</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.151652</td>\n",
       "      <td>True</td>\n",
       "      <td>8.274524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.565327</td>\n",
       "      <td>True</td>\n",
       "      <td>5.615925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.943763</td>\n",
       "      <td>True</td>\n",
       "      <td>9.943361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.025108</td>\n",
       "      <td>True</td>\n",
       "      <td>6.526274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-1.452482</td>\n",
       "      <td>False</td>\n",
       "      <td>-1.081065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>1.532964</td>\n",
       "      <td>False</td>\n",
       "      <td>2.817101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>-0.350965</td>\n",
       "      <td>True</td>\n",
       "      <td>1.761346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-1.642007</td>\n",
       "      <td>False</td>\n",
       "      <td>-3.852874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>0.641072</td>\n",
       "      <td>False</td>\n",
       "      <td>2.620429</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0     v0         y\n",
       "0    0.883800   True  6.032204\n",
       "1    2.151652   True  8.274524\n",
       "2    0.565327   True  5.615925\n",
       "3    1.943763   True  9.943361\n",
       "4    1.025108   True  6.526274\n",
       "..        ...    ...       ...\n",
       "995 -1.452482  False -1.081065\n",
       "996  1.532964  False  2.817101\n",
       "997 -0.350965   True  1.761346\n",
       "998 -1.642007  False -3.852874\n",
       "999  0.641072  False  2.620429\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = dowhy.datasets.linear_dataset(beta=5,\n",
    "        num_common_causes=1,\n",
    "        num_instruments = 0,\n",
    "        num_samples=1000,\n",
    "        treatment_is_binary=True)\n",
    "df = data['df']\n",
    "df['y'] = df['y'] + np.random.normal(size=len(df)) # Adding noise to data. Without noise, the variance in Y|X, Z is zero, and mcmc fails.\n",
    "#data['dot_graph'] = 'digraph { v ->y;X0-> v;X0-> y;}'\n",
    "\n",
    "treatment= data[\"treatment_name\"][0]\n",
    "outcome = data[\"outcome_name\"][0]\n",
    "common_cause = data[\"common_causes_names\"][0]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='v0'>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAEWCAYAAABPON1ZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAN7UlEQVR4nO3df4xl9V3G8edhfzgU1pIOF2MZ6KxlU0OrdXFCNRgMbCpUmjbERktc07ol+081NdUYGpRoq4ZqNGCCJFtskYjdSluSplQsUUhDwiKzLCosonVDyyCww5bGBVxY2Mc/7h12dhmYM80993yY+34lN3t/nL3388fMO2e+95x7nUQAgLpO6HoAAMDrI9QAUByhBoDiCDUAFEeoAaA4Qg0Axa1t40lPPfXUTE9Pt/HUALAq7d69++kkvaUeayXU09PTmp2dbeOpAWBVsv2d13qMpQ8AKI5QA0BxhBoAimtljXophw8f1tzcnA4dOjSql1yRiYkJTU1Nad26dV2PAgDHGFmo5+bmtGHDBk1PT8v2qF62kSQ6cOCA5ubmtHHjxq7HAYBjjGzp49ChQ5qcnCwXaUmyrcnJybJ7+wDG20jXqCtGekHl2QCMN95MBIDiRrZGfbzpK24b6vM9evUlQ30+oEvD/v0Yd2/0PozNHvVVV12la6655pXbV155pa699truBgKAhsYm1Nu2bdNNN90kSTpy5Ih27typrVu3djwVACyvs6WPUZuentbk5KT27Nmjp556Sps3b9bk5GTXYwHAssYm1JJ0+eWX68Ybb9STT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Ti26eoP6y3P90NM7YYI+6pl9R/3C8jyV5Uv1v1fmzbkcCJPX/slu4/JCk28SXXbeONxMBNDI40eWzSX6n61nGDUsfhdg+qMFJLsc/JClJfnjEIwGSJNtrB9/2dF7Xs4wj9qgBLMv2/UnOsX29pNMl3SLpuYXHk3y1s+HGAHvUhdk+Tf1jVSVxUgFKmJB0QNKFOno8dSQR6hYR6oJsf0DSn0t6q6T9kt4m6WFJnFSArpw2OOLjQR0N9AL+LG8ZR33U9Bn1Tx//zyQb1f9chV3djoQxt0bSyYPLhkXXFy5oEXvUNR1OcsD2CbZPSHKn7Wu6Hgpj7Ykkn+56iHFFqGv6vu2TJX1L0s2292vRGzdAB/g89A5x1Echts9M8l3bJ0n6P/WXpn5V0psl3ZzkQKcDYmzZfkuS73U9x7gi1IUsHAI1uP6VJL/U9UwAusebibUs/vPyxzqbAkAphLqWvMZ1AGOMpY9CbL+s/puGlnSipOcXHhKnkANji1ADQHEsfQBAcYQaAIoj1Bgrtj9i+78Gl490PQ/QBGvUGBu23yJpVtKM+kfV7Jb000me6XQwYBmcQo5VyfbVkh4bfDGwbP+BpJcl3bFwhp3tOyRdLOmLXc0JNMHSB1arL0n65UW3F64/tui+OfU/BB8ojT1qrEpJ9tg+zfZbJfUkPaP+56dMvP7/BOphjxqr2S2SPqT+t7p/SdLjks5Y9PjU4D6gNN5MxKpl+52SPifpVEk/L+kF9d9APGewyf3qv5nIp8KhNJY+sGolecj2BkmPJ3lCkmx/RtJ9g00+TaTxRsAeNQAUxxo1ABRHqAGgOEINAMURagAojlADQHGEGgCKI9QAUByhBoDi/h84tCMtUDB++gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# data['df'] is just a regular pandas.DataFrame\n",
    "df.causal.do(x=treatment,\n",
    "                     variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'},\n",
    "                     outcome=outcome,\n",
    "                     common_causes=[common_cause],\n",
    "                     proceed_when_unidentifiable=True).groupby(treatment).mean().plot(y=outcome, kind='bar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='v0'>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAERCAYAAABSPe3hAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAMQ0lEQVR4nO3db4ylZ1nH8d/F7jarsrGyHYyy1GlCgxYMto7lBQbTNkoRoyEhCkkNcSX7QiUYNQZDbPwTDb4xrQkxWQErEalWICEQK41CDAktnW2r0FYiadBOpXRYIAKx0HYvX8xMO61T5szuOXvuPfv5JJPOM+eZZ68Xm2/v3HOf2eruADCu58x7AAC+PaEGGJxQAwxOqAEGJ9QAg9s/i4dedNFFvby8PItHAyykEydOfKm7l3Z6bSahXl5ezurq6iweDbCQquo/n+01Wx8AgxNqgMEJNcDgZrJHDTAvjz32WNbW1vLoo4/Oe5QdHTx4MEeOHMmBAwcm/h6hBhbK2tpaDh06lOXl5VTVvMd5mu7OyZMns7a2lksuuWTi77P1ASyURx99NIcPHx4u0klSVTl8+PCeV/tCDSycESO95XRmE2qAwdmjBhba8ls/MtXnff7tr5nq8yYh1IOb9l8ymKZ5ROt8ZOsDYIquv/763HDDDU9ev+1tb8uNN954Rs8UaoApOnr0aN7znvckSU6dOpWbb74511133Rk909YHwBQtLy/n8OHDufvuu/PFL34xl19+eQ4fPnxGzxRqgCl705velJtuuikPP/xwjh49esbPs/UBMGWvfe1rc+utt+bOO+/Mq171qjN+nhU1sNDmcTLlggsuyFVXXZULL7ww+/btO+PnCTXAlJ06d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u976/MFtX1tL8oKzNhGcJitqFlJ3311Vz6+q70+ylOQrSf43G7+kCc4pVtQssluSvC7JL2Rjhf1Qkhdue/3I5tdgaH6YyMKqqpck+YskFyX5iSTfzMYPELd+B8hd2fhhoncwMjRbHyys7r63qg4leai7v5AkVfWHSe7cvOUPRJpzgRU1wODsUQMMTqgBBifUAIMTaoDBCTXA4IQaYHBCDTC4/wNMrZa1m17lQQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df.causal.do(x={treatment: 1}, \n",
    "              variable_types={treatment:'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome,\n",
    "              method='weighting', \n",
    "              common_causes=[common_cause],\n",
    "              proceed_when_unidentifiable=True).groupby(treatment).mean().plot(y=outcome, kind='bar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "cdf_1 = df.causal.do(x={treatment: 1}, \n",
    "              variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome, \n",
    "              dot_graph=data['dot_graph'],\n",
    "              proceed_when_unidentifiable=True)\n",
    "\n",
    "cdf_0 = df.causal.do(x={treatment: 0}, \n",
    "              variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome, \n",
    "              dot_graph=data['dot_graph'],\n",
    "              proceed_when_unidentifiable=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "      <th>propensity_score</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.117996</td>\n",
       "      <td>False</td>\n",
       "      <td>2.497059</td>\n",
       "      <td>0.333225</td>\n",
       "      <td>3.000976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.238462</td>\n",
       "      <td>False</td>\n",
       "      <td>0.509070</td>\n",
       "      <td>0.455258</td>\n",
       "      <td>2.196556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.213945</td>\n",
       "      <td>False</td>\n",
       "      <td>2.279505</td>\n",
       "      <td>0.458815</td>\n",
       "      <td>2.179528</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.425245</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.297296</td>\n",
       "      <td>0.428336</td>\n",
       "      <td>2.334615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.301323</td>\n",
       "      <td>False</td>\n",
       "      <td>3.431520</td>\n",
       "      <td>0.200139</td>\n",
       "      <td>4.996525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>0.621286</td>\n",
       "      <td>False</td>\n",
       "      <td>1.458936</td>\n",
       "      <td>0.400531</td>\n",
       "      <td>2.496683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>1.642750</td>\n",
       "      <td>False</td>\n",
       "      <td>3.409962</td>\n",
       "      <td>0.268860</td>\n",
       "      <td>3.719407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>0.409933</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.635569</td>\n",
       "      <td>0.430529</td>\n",
       "      <td>2.322722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-0.063154</td>\n",
       "      <td>False</td>\n",
       "      <td>0.953209</td>\n",
       "      <td>0.499221</td>\n",
       "      <td>2.003123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>1.532964</td>\n",
       "      <td>False</td>\n",
       "      <td>2.817101</td>\n",
       "      <td>0.281662</td>\n",
       "      <td>3.550353</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0     v0         y  propensity_score    weight\n",
       "0    1.117996  False  2.497059          0.333225  3.000976\n",
       "1    0.238462  False  0.509070          0.455258  2.196556\n",
       "2    0.213945  False  2.279505          0.458815  2.179528\n",
       "3    0.425245  False -0.297296          0.428336  2.334615\n",
       "4    2.301323  False  3.431520          0.200139  4.996525\n",
       "..        ...    ...       ...               ...       ...\n",
       "995  0.621286  False  1.458936          0.400531  2.496683\n",
       "996  1.642750  False  3.409962          0.268860  3.719407\n",
       "997  0.409933  False -0.635569          0.430529  2.322722\n",
       "998 -0.063154  False  0.953209          0.499221  2.003123\n",
       "999  1.532964  False  2.817101          0.281662  3.550353\n",
       "\n",
       "[1000 rows x 5 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cdf_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "      <th>propensity_score</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.055191</td>\n",
       "      <td>True</td>\n",
       "      <td>5.738184</td>\n",
       "      <td>0.658568</td>\n",
       "      <td>1.518446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.676538</td>\n",
       "      <td>True</td>\n",
       "      <td>7.187013</td>\n",
       "      <td>0.735005</td>\n",
       "      <td>1.360535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.111945</td>\n",
       "      <td>True</td>\n",
       "      <td>4.353155</td>\n",
       "      <td>0.526346</td>\n",
       "      <td>1.899889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.840229</td>\n",
       "      <td>True</td>\n",
       "      <td>3.503758</td>\n",
       "      <td>0.389082</td>\n",
       "      <td>2.570151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.556640</td>\n",
       "      <td>True</td>\n",
       "      <td>5.125285</td>\n",
       "      <td>0.590361</td>\n",
       "      <td>1.693878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>0.018338</td>\n",
       "      <td>True</td>\n",
       "      <td>6.145009</td>\n",
       "      <td>0.512687</td>\n",
       "      <td>1.950507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>0.094625</td>\n",
       "      <td>True</td>\n",
       "      <td>5.329991</td>\n",
       "      <td>0.523821</td>\n",
       "      <td>1.909048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>0.711712</td>\n",
       "      <td>True</td>\n",
       "      <td>4.394407</td>\n",
       "      <td>0.612092</td>\n",
       "      <td>1.633741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>1.928415</td>\n",
       "      <td>True</td>\n",
       "      <td>8.157832</td>\n",
       "      <td>0.762678</td>\n",
       "      <td>1.311169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>-0.280647</td>\n",
       "      <td>True</td>\n",
       "      <td>5.377214</td>\n",
       "      <td>0.469032</td>\n",
       "      <td>2.132052</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0    v0         y  propensity_score    weight\n",
       "0    1.055191  True  5.738184          0.658568  1.518446\n",
       "1    1.676538  True  7.187013          0.735005  1.360535\n",
       "2    0.111945  True  4.353155          0.526346  1.899889\n",
       "3   -0.840229  True  3.503758          0.389082  2.570151\n",
       "4    0.556640  True  5.125285          0.590361  1.693878\n",
       "..        ...   ...       ...               ...       ...\n",
       "995  0.018338  True  6.145009          0.512687  1.950507\n",
       "996  0.094625  True  5.329991          0.523821  1.909048\n",
       "997  0.711712  True  4.394407          0.612092  1.633741\n",
       "998  1.928415  True  8.157832          0.762678  1.311169\n",
       "999 -0.280647  True  5.377214          0.469032  2.132052\n",
       "\n",
       "[1000 rows x 5 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cdf_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparing the estimate to Linear Regression\n",
    "First, estimating the effect using the causal data frame, and the 95% confidence interval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANEAAAASCAYAAADFRjv1AAAHDUlEQVR4nO3ba6xdRRUH8F+hiMqjPmk1PqDVao2PVhStCoLIVVttwEc0BkRjawySWrQErVZuMYSiItY3vniUBKNSX6GiWBtrxUdUjBIESu0t1IpQ4ZaWtggFP6zZufvuu/e5Z/a5fvL8k5M5Z/bsWeu/7sysNWvmThocHNRHH320xwGV30N4pOFzZ4v+n4ZvYTseSP1/Do+vaTsJi/A77Mb9+APeX6MnPBEL8X3chr3YiY14b8M7Q/L45epUIId3FaeWdFrY0KatXjly2ti3wInpvTsF/+34KeY1tM+111vxBfwK9yUOV3bQBy7EOtyRuNyDG3Cu4FpGFvfJNcJ2JgJV7B5HySpm4HocgR/iZhyDD+L1eCX+XWp/Jd6Ju3AV9uAkfAWvwLsq/b8tPfsn1uN2TMWb8Q28IbV5pAd+uTq14V3G0/HFpMuhDW3a6pUrp619P4WzsQ0/wg48GUfjeKyttG9jr4/jRUn/bXjuOHzhLPwJ1wm7HYKXYxDvS9/vaMO9bhINp457xZeFYRaLVaPAZxOh88XKCaeIQbFFGHBHqn8UrsZp+AHWlPq5FQtwDR4u1S/D7/EWQfrqil7DuuPXRifyeJcxCZeKAbMGSydYr1w5bey7SEygy8XA/E+lz4Nq5LSx11li8tyGV4uBPh4Ox76a+vMFp4/ijFSXxb1b15+LGRgQbvlLlWfnivDjNLEaEAMDLjIyKIg/wvL0/cxKP7/Aj40mSYQQX03fj8/WfARtdMrlXcZivAbvSe0mUq82cnLte7AYkLern0DwYOV3W3utxyZjvWAn1E0g+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wq1hd9mQ5zsMfk6xOIVwbfrk65fJuutYzKAb35eKEvIocvfb2ICfHvlvF2coCkda+uPRsQOzhho3O6LUZJ20wU4Rv1QTGAfikSIpcb8RbksG9PIneLjZOG4RBdolJMl+ECWvxmcr76/BMkW4dqjw7Iyn2eeEG/yY2uCcI9/yxSvvrxB/8xiR7VpK9V+wBtlfan55I7hd3qBbX8BvCZT3wy9WpDe82aKNXLnLtS0yEOeK2wXyxPzoKJ6d+Fho7kNvY6+T0YSRRMLekyw6jb2LMwwXC424RtzWmikTHdHGIuqjUPot7eRKtF3n7OcKNHiJWjo3ifGG1vBPizSIPf564AzVP3EVaJVaZeyvtvyfCkVNF3PwPfE2Q31bTf3FOcqD6OJ4497ksfW/DL1cn8nm3QRu9cpFrX0n20SKdvkAkau4THuoCcWWmijb2mi0GehnTjZyTbTV6Ev0czxIp7TkiZX6/mKSrxQQue5os7pP6/wrRRx+94X91d66PPv5v0J9EffTRI/4LHNPNtgrpX74AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 5.092590494260132$"
      ],
      "text/plain": [
       "5.092590494260132"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(cdf_1['y'] - cdf_0['y']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOwAAAASCAYAAABYSzJ9AAAGl0lEQVR4nO3be6weRRkG8F9JVVQQDIqN0RiooA1BCxoUpUK9NCgYW29RA4qJVgMGgzaIF+yphggatNRbEMUbBqIV1FgvFSQRvGAMNMagUlpEWgSFolRbwBT9450vZ89+3+7ZmbPnaJPvSb7M6e7MvvM+z+zMO+9s501MTBhjjDH2Duwz4tpTcCnuxIP4E9bi8ZnPfi0+jetwP/6Dyxrqnpbut/32dLB5SqX+23q0cxI2Yht2Yyu+hWNb+lLCY06bEl/m4e24Af/Ev/AbvNPos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iJzh1xvy87J4i0/vfEzLxVHAXcKJIY2/FeU8OoEh774H46X/aIfec5YlV8S/ptTu12pnr1FaOOu3GVSPocJHSYCUq0LOGrxP8TdNd/TsZk9YX9Yyqb9kmDbGDTvqFPTJds2k/0c5HIYlYPmVenOpekf6+dgZ2TUznqWGIXfi04PKpyvYTHPrjvkqD7txiAR2JfEXYtFyHfYWKfd1uLjSpuFyvVEYYTMjko0bKUr1z/c/SfkzFZ3cMOOrUsdaIaGuwvDqJ34VctxvrAviIlv0d0ehQebLl3tCDweiFsU2jSxc6jUtmUuh9cf6hyrYTHmXLfxZc2vEFkRy/PbPfkVHb5Cq0JJVr2PVab/M/Rf07GZPWF3SIyX8twhqmH0WtE7H6xqWdKC8WmfYuYvfrA68TG//uak027NX+uNiHI+arm7HJXO9eJs7mVwvftlXsvFwPjAfHFzQAlPJa0yfWF+Aji/tq1xfgE7jOc8T1chMD15M4+Ikt8sPD9vhab06FEy1K+cv3P0X9OxmT9S6fTk/F1eAl+LzbSS0V48cFa/WvE4fIhIqyoYnn6MXkGdqzYvBPhx6ivPwYhQduXTX2gi5314pztpYKLq3CXCHtOFkmYcwx/ApfLY2mbHF+IY4jd+J3Ysy0S38nuFvu7O2v1XyGOMa4XoeK9IlN8vNi33WVq5nSA5cq0z0EJX7n+l+pfiml1rL+wW/Bc8bnViUKwv4hs2hp5M+lisamv4lCTCZrbDYu2SKTDmxInfaGrnYcFB2eIsGmFOJDfkdqtEzN9HSU8lnKfw9n65Mcp4qxvu8mzxW0j6l8tvps9TqwQB4pV6xZx1rjO5PFIFYvla5+LEr5y/S/VvwSddJw3/u91Y4yx9+D/+Rx2jDHGqGH8wo4xxl6E/wI6U/UPXeefgQAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 0.17478007931409847$"
      ],
      "text/plain": [
       "0.17478007931409847"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1.96*(cdf_1['y'] - cdf_0['y']).std() / np.sqrt(len(df))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparing to the estimate from OLS."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>            <td>y</td>        <th>  R-squared (uncentered):</th>      <td>   0.970</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared (uncentered):</th> <td>   0.970</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th>          <td>1.620e+04</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Tue, 02 Mar 2021</td> <th>  Prob (F-statistic):</th>           <td>  0.00</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>20:29:26</td>     <th>  Log-Likelihood:    </th>          <td> -1414.1</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>  1000</td>      <th>  AIC:               </th>          <td>   2832.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>   998</td>      <th>  BIC:               </th>          <td>   2842.</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     2</td>      <th>                     </th>              <td> </td>    \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>              <td> </td>    \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x1</th> <td>    1.7444</td> <td>    0.031</td> <td>   55.843</td> <td> 0.000</td> <td>    1.683</td> <td>    1.806</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x2</th> <td>    5.0338</td> <td>    0.052</td> <td>   97.742</td> <td> 0.000</td> <td>    4.933</td> <td>    5.135</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td> 4.501</td> <th>  Durbin-Watson:     </th> <td>   1.900</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.105</td> <th>  Jarque-Bera (JB):  </th> <td>   4.538</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td> 0.147</td> <th>  Prob(JB):          </th> <td>   0.103</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 2.851</td> <th>  Cond. No.          </th> <td>    2.50</td>\n",
       "</tr>\n",
       "</table><br/><br/>Notes:<br/>[1] R² is computed without centering (uncentered) since the model does not contain a constant.<br/>[2] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                                 OLS Regression Results                                \n",
       "=======================================================================================\n",
       "Dep. Variable:                      y   R-squared (uncentered):                   0.970\n",
       "Model:                            OLS   Adj. R-squared (uncentered):              0.970\n",
       "Method:                 Least Squares   F-statistic:                          1.620e+04\n",
       "Date:                Tue, 02 Mar 2021   Prob (F-statistic):                        0.00\n",
       "Time:                        20:29:26   Log-Likelihood:                         -1414.1\n",
       "No. Observations:                1000   AIC:                                      2832.\n",
       "Df Residuals:                     998   BIC:                                      2842.\n",
       "Df Model:                           2                                                  \n",
       "Covariance Type:            nonrobust                                                  \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "x1             1.7444      0.031     55.843      0.000       1.683       1.806\n",
       "x2             5.0338      0.052     97.742      0.000       4.933       5.135\n",
       "==============================================================================\n",
       "Omnibus:                        4.501   Durbin-Watson:                   1.900\n",
       "Prob(Omnibus):                  0.105   Jarque-Bera (JB):                4.538\n",
       "Skew:                           0.147   Prob(JB):                        0.103\n",
       "Kurtosis:                       2.851   Cond. No.                         2.50\n",
       "==============================================================================\n",
       "\n",
       "Notes:\n",
       "[1] R² is computed without centering (uncentered) since the model does not contain a constant.\n",
       "[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = OLS(np.asarray(df[outcome]), np.asarray(df[[common_cause, treatment]], dtype=np.float64))\n",
    "result = model.fit()\n",
    "result.summary()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
